String Function

Course- C IN LINUX >

Some programming languages like Java and C++ have a string data type that hides some of the complexity underneath what might seem a simple thing.

An essential attribute of a character string is that it is a series of individual character elements of indeterminate length.

Most of the individual characters we can type into a keyboard are represented by simple numerical ASCII codes and the C data type char is used to store character data.

Strings are stored as arrays of characters ending with a NULL so an array must be large enough to hold the sequence of characters plus one. Remember array members are always counted from zero.

In this example we can see 5 individual characters declared and initialised with values, and an empty character array set to “”.

Take care to notice the diference between single quote marks ‘ used around characters and double quote marks “ used around character strings.

Try this...

#include<stdio.h>


int main(int argc, char *argv[], char *env[])
{

char c1='s';
char c2='a';
char c3='n';
char c4='d';
char c5='y';
char name[6]="";
sprintf(name, "%c%c%c%c%c,c1,c2,c3,c4,c5);
printf("%s\n",name);

return 0;
}                    
                    

Out put: sandy